package site.marqstree.latte.ec.main.personal.address;

import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.util.List;

import site.marqstree.latte.core.net.RestClient;
import site.marqstree.latte.core.net.callback.ISuccess;
import site.marqstree.latte.ec.R;
import site.marqstree.latte.ec.profile.AppProfile;
import site.marqstree.latte.ui.recycler.MultipleFields;
import site.marqstree.latte.ui.recycler.MultipleItemEntity;

/*
 * 项目名: EleCommerce
 * 包名: site.marqstree.latte.ec.main.personal.address
 * 文件名: AddressAdapter
 * 创建者: marqstree
 * 创建时间: 2020/2/5 19:29
 * 描述: TODO
 */
public class AddressAdapter extends BaseMultiItemQuickAdapter<MultipleItemEntity, BaseViewHolder> {

    protected AddressAdapter() {
        super();
        addItemType(AddressItemType.ITEM_ADDRESS, R.layout.item_address);
    }

    @Override
    protected void convert(BaseViewHolder holder, MultipleItemEntity entity) {
        switch (holder.getItemViewType()) {
            case AddressItemType.ITEM_ADDRESS:
                final String name = entity.getField(MultipleFields.NAME);
                final String phone = entity.getField(AddressItemFields.PHONE);
                final String address = entity.getField(AddressItemFields.ADDRESS);
                final boolean isDefault = entity.getField(MultipleFields.TAG);
                final int id = entity.getField(MultipleFields.ID);

                final TextView nameText = holder.getView(R.id.tv_address_name);
                final TextView phoneText = holder.getView(R.id.tv_address_phone);
                final TextView addressText = holder.getView(R.id.tv_address_address);
                final TextView deleteTextView = holder.getView(R.id.tv_address_delete);
                deleteTextView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        RestClient.builder()
                                .url(AppProfile.DELETE_ADDRESS_URL)
                                .params("id", id)
                                .success(new ISuccess() {
                                    @Override
                                    public void onSuccess(String response) {
                                        //删除一行数据
                                        remove(holder.getLayoutPosition());
                                    }
                                })
                                .build()
                                .post();
                    }
                });

                nameText.setText(name);
                phoneText.setText(phone);
                addressText.setText(address);
                break;
            default:
                break;
        }
    }
}
